#region Using Directives

using System;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.VisualStudio.Modeling;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using Microsoft.Practices.RecipeFramework.Services;
using Microsoft.Practices.RecipeFramework.VisualStudio;
using Microsoft.Practices.RecipeFramework.VisualStudio.Templates;
using EnvDTE;
using EastBancTech.InstallerDefinition;
using Microsoft.Practices.Modeling.Dsl.Integration.Helpers;
using WixPackageDsl.Helper;

#endregion

namespace EBT.Deployment.SoftwareFactory.Actions
{
    public class AddIISHostToModelAction: ActionBase
    {
        static public readonly String DefaultWebSiteName = "Default Web Site";
        static public readonly String DefaultAppPoolName = "DefaultAppPool";
        static public readonly Int32 DefaultWebSitePort = 80;

        Store wixPackageStore;
        String webSiteName = DefaultWebSiteName;
        String appPoolName = DefaultAppPoolName;
        Int32 webSitePort = DefaultWebSitePort;

        IISApplicationPool appPool;
        WebSite webSite;
        

        [Input(Required = true)]
        public Int32 WebSitePort
        {
            get { return webSitePort; }
            set { webSitePort = value; }
        }

        [Output]
        public IISApplicationPool AppPool
        {
            get { return appPool; }
            set { appPool = value; }
        }

        [Output]
        public WebSite WebSite
        {
            get { return webSite; }
            set { webSite = value; }
        }

        [Input(Required = true)]
        public String WebSiteName
        {
            get { return webSiteName; }
            set { webSiteName = value; }
        }

        [Input(Required = true)]
        public String AppPoolName
        {
            get { return appPoolName; }
            set { appPoolName = value; }
        }

        [Input(Required = true)]
        public Store WixPackageStore
        {
            get { return wixPackageStore; }
            set { wixPackageStore = value; }
        }

        
        
        protected override void OnExecute()
        {
            InstallerPackageModel cmpPackage = DomainModelHelper.GetElement<InstallerPackageModel>(WixPackageStore) as InstallerPackageModel;

            if (cmpPackage == null)
                throw new ApplicationException("Current DSL model does not contain 'InstallerPackageModel'");

            IIS iis = DomainModelHelper.GetElement<IIS>(WixPackageStore) as IIS;

            using (Transaction transaction = WixPackageStore.TransactionManager.BeginTransaction("Add new IIS Host and Pool"))
            {
                if (iis == null)
                {
                    iis = WixPackageStore.ElementFactory.CreateElement(IIS.DomainClassId) as IIS;
                    iis.Name = "IIS";
                    iis.Title = "IIS";
                    iis.Description = "Default IIS Server";
                    DomainModelHelper.AddLinkedElement(cmpPackage.ApplicationHost, iis as ComponentHost);
                }

                //Create default web site if it's not exsisted
                //We should move this logic into Dsl as custom action after IIS element is created
                webSite = ModelHelper.FindElementsByName<WebSite>(WixPackageStore, DefaultWebSiteName);
                if (webSite == null)
                {
                    webSite = wixPackageStore.ElementFactory.CreateElement(WebSite.DomainClassId) as WebSite;
                    webSite.Name = DefaultWebSiteName;
                    webSite.Port = DefaultWebSitePort;

                    VirtualDirectoryProperties vProp = wixPackageStore.ElementFactory.CreateElement(VirtualDirectoryProperties.DomainClassId) as VirtualDirectoryProperties;
                    vProp.Name = String.Format("{0}_DirProperties", DefaultWebSiteName);

                    DomainModelHelper.AddLinkedElement(iis.WebSites, webSite);
                }

                //Create default application pool if it's not exsisted
                //We should move this logic into Dsl as custom action after IIS element is created
                appPool = ModelHelper.FindElementsByName<IISApplicationPool>(WixPackageStore, DefaultAppPoolName);
                if (appPool == null)
                {
                    appPool = wixPackageStore.ElementFactory.CreateElement(IISApplicationPool.DomainClassId) as IISApplicationPool;
                    appPool.Name = DefaultAppPoolName;

                    DomainModelHelper.AddLinkedElement(iis.AppPools, appPool);
                }

                appPool = ModelHelper.FindElementsByName<IISApplicationPool>(WixPackageStore, AppPoolName);
                if (appPool == null)
                {
                    appPool = wixPackageStore.ElementFactory.CreateElement(IISApplicationPool.DomainClassId) as IISApplicationPool;
                    appPool.Name = AppPoolName;
                    appPool.Identity = IdentityType.other;
                    DomainModelHelper.AddLinkedElement(iis.AppPools, appPool);
                }

                webSite = ModelHelper.FindElementsByName<WebSite>(WixPackageStore, WebSiteName);
                if (webSite == null)
                {
                    webSite = wixPackageStore.ElementFactory.CreateElement(WebSite.DomainClassId) as WebSite;
                    webSite.Name = WebSiteName;
                    webSite.Port = webSitePort;
                    webSite.Description = WebSiteName;

                    VirtualDirectoryProperties vProp = wixPackageStore.ElementFactory.CreateElement(VirtualDirectoryProperties.DomainClassId) as VirtualDirectoryProperties;
                    vProp.Name = String.Format("{0}_DirProperties", WebSiteName);
                    DomainModelHelper.AddLinkedElement(cmpPackage.Definitions.VirtualDirectoryProperties, vProp);

                    webSite.DirProperties = vProp;
                    DomainModelHelper.AddLinkedElement(iis.WebSites, webSite);
                }

                transaction.Commit();                
            }
        }

       
    }
}
